import { WS, WS_DATA, WS_PIC_DATA } from '@/store/mutation-types'

const baseUrl = window.PROJECT_CONFIG.VUE_APP_SOCKET_API_BASE_URL
export default {
  data() {
    return {
      WS: null,
      data: []
    }
  },
  created() {
    this.WS = new WebSocket(baseUrl)
    const that = this
    this.WS.onmessage = function(evt) {
      if (evt.data !== 'connected successfully' && evt.data) {
        that.data = JSON.parse(evt.data) ? JSON.parse(evt.data).realtimeData : []
      } else {
        that.data = []
      }
    }
  },
  methods: {
    sendData(pageId) {
      const that = this
      that.data = []
      that.WS.send(
        JSON.stringify({
          pageId
        })
      )
    }
  },
  beforeDestroy() {
    this.WS.onmessage = null
    this.WS.close();
    this.WS = null
  }
}